using OrderSystem.SystemFramework;
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace BarcodePrinting
{
	public class Printer_General_1 : BarcodePrinter
	{
		private LabelModel[] _labels;

		private PrintDocument _printDocument;

		private PrintPreviewDialog _printPreviewDialog1;

		private PrintDialog _printDialog1;

		private string _sBarcodePrinterName = "0";

		private int _nLabelWidth = 60;

		private int _nLabelHeight = 30;

		private int _nColumnCountOfPage = 1;

		private int _nHorizontalGap = 2;

		private string _sPrintWay = "0";

		private string _sPrintLogo = "0";

		private float _nOffsetX = 0f;

		private float _nOffsetY = 0f;

		private float _nFontSize = 0f;

		private bool _不同类型是否换行打印;

		private int i = 0;

		private int j = 0;

		private bool _isDisposed = false;

		public Printer_General_1()
		{
			this._sBarcodePrinterName = AppSettings.GetAppSettings("barcodePrinterName");
			this._nLabelWidth = 50;
			this._nLabelHeight = 30;
			this._nColumnCountOfPage = 2;
			this._nHorizontalGap = 2;
			this._sPrintWay = AppSettings.GetAppSettings("printWay");
			this._sPrintLogo = AppSettings.GetAppSettings("printLogo");
			this._nOffsetX = Convert.ToSingle(AppSettings.GetAppSettings("OffsetX"));
			this._nOffsetY = Convert.ToSingle(AppSettings.GetAppSettings("OffsetY"));
			this._nFontSize = Convert.ToSingle(AppSettings.GetAppSettings("FontSize"));
			this._不同类型是否换行打印 = false;
		}

		public override void Print(LabelModel[] labels)
		{
			this._labels = labels;
			this._printDocument = new PrintDocument();
			this._printDocument.PrinterSettings.PrinterName = this._sBarcodePrinterName;
			if (!this._printDocument.PrinterSettings.IsValid)
			{
				throw new Exception("指定了一个无效的打印机！");
			}
			this._printDocument.DocumentName = this._labels[0].ProductID.Split(new char[]
			{
				'-'
			}).GetValue(0).ToString();
			this._printDocument.DefaultPageSettings.PaperSize = new PaperSize("SpecimenLabel", Convert.ToInt32((double)(this._nLabelWidth * this._nColumnCountOfPage + this._nHorizontalGap * (this._nColumnCountOfPage - 1)) / 25.4 * 100.0), Convert.ToInt32((double)this._nLabelHeight / 25.4 * 100.0));
			this._printDocument.DefaultPageSettings.Landscape = false;
			this._printDocument.QueryPageSettings += new QueryPageSettingsEventHandler(this.printDocument_QueryPageSettings);
			this._printDocument.BeginPrint += new PrintEventHandler(this.printDocument_BeginPrint);
			this._printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);
			this._printDocument.Print();
			this._printDocument.Dispose();
			this._printDocument = null;
		}

		public override void PrintPreview(LabelModel[] labels)
		{
			this._labels = labels;
			this._printDocument = new PrintDocument();
			this._printDocument.PrinterSettings.PrinterName = this._sBarcodePrinterName;
			if (!this._printDocument.PrinterSettings.IsValid)
			{
				throw new Exception("指定了一个无效的打印机！");
			}
			this._printDocument.DocumentName = this._labels[0].ProductID.Split(new char[]
			{
				'-'
			}).GetValue(0).ToString();
			this._printDocument.DefaultPageSettings.PaperSize = new PaperSize("SpecimenLabel", Convert.ToInt32((double)(this._nLabelWidth * this._nColumnCountOfPage + this._nHorizontalGap * (this._nColumnCountOfPage - 1)) / 25.4 * 100.0), Convert.ToInt32((double)this._nLabelHeight / 25.4 * 100.0));
			this._printDocument.DefaultPageSettings.Landscape = false;
			this._printDocument.QueryPageSettings += new QueryPageSettingsEventHandler(this.printDocument_QueryPageSettings);
			this._printDocument.BeginPrint += new PrintEventHandler(this.printDocument_BeginPrint);
			this._printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);
			if (this._sPrintWay == "1")
			{
				this._printDocument.Print();
			}
			else if (this._sPrintWay == "2")
			{
				this._printPreviewDialog1 = new PrintPreviewDialog();
				this._printPreviewDialog1.Document = this._printDocument;
				this._printPreviewDialog1.WindowState = FormWindowState.Maximized;
				this._printPreviewDialog1.ShowDialog();
				if (this._printPreviewDialog1 != null)
				{
					this._printPreviewDialog1.Dispose();
					this._printPreviewDialog1 = null;
				}
			}
			else if (this._sPrintWay == "3")
			{
				this._printDialog1 = new PrintDialog();
				this._printDialog1.Document = this._printDocument;
				this._printDialog1.AllowCurrentPage = true;
				this._printDialog1.AllowSelection = true;
				this._printDialog1.AllowSomePages = true;
				this._printDialog1.ShowHelp = true;
				this._printDialog1.ShowNetwork = true;
				if (this._printDialog1.ShowDialog() == DialogResult.OK)
				{
					this._printDocument.Print();
				}
				if (this._printDialog1 != null)
				{
					this._printDialog1.Dispose();
					this._printDialog1 = null;
				}
			}
			this._printDocument.Dispose();
			this._printDocument = null;
		}

		private void printDocument_BeginPrint(object sender, PrintEventArgs e)
		{
			this.i = 0;
			this.j = 0;
		}

		private void printDocument_QueryPageSettings(object sender, QueryPageSettingsEventArgs e)
		{
		}

		private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
		{
			e.Graphics.PageUnit = GraphicsUnit.Millimeter;
			bool is换行 = false;
			int nColumnIndex = 0;
			int nLabelsOfItem = this._labels[this.i].PrintQuantity * this._labels[this.i].PrintMultiple;
			while (this.j < nLabelsOfItem)
			{
				nColumnIndex++;
				string sData = ((this._labels[this.i].LabelName == "") ? this._labels[this.i].ProjectName : this._labels[this.i].LabelName) + " " + this._labels[this.i].ProductID.Substring(4);
				Font f = new Font("黑体", 4.5f + this._nFontSize, FontStyle.Bold, GraphicsUnit.Millimeter);
				float fStringWidth = 0f;
				f = this.CalculateFitFont(sData, (float)(this._nLabelWidth - 2), f, ref fStringWidth, e.Graphics);
				e.Graphics.DrawString(sData, f, Brushes.Black, 0f + (float)((this._nLabelWidth + this._nHorizontalGap) * (nColumnIndex - 1)) + this._nOffsetX, 0f + this._nOffsetY);
				string sProductTypeData = this._labels[this.i].ProductType;
				f.Dispose();
				f = new Font("黑体", 5f + this._nFontSize, FontStyle.Bold, GraphicsUnit.Millimeter);
				fStringWidth = 0f;
				f = this.CalculateFitFont(sProductTypeData, (float)(this._nLabelWidth - 2), f, ref fStringWidth, e.Graphics);
				e.Graphics.DrawString(sProductTypeData, f, Brushes.Black, 0f + (float)((this._nLabelWidth + this._nHorizontalGap) * (nColumnIndex - 1)) + this._nOffsetX, 5.5f + this._nOffsetY);
				sData = this._labels[this.i].ProductCode;
				f.Dispose();
				f = new Font("黑体", 4.3f + this._nFontSize, FontStyle.Bold, GraphicsUnit.Millimeter);
				fStringWidth = 0f;
				f = this.CalculateFitFont(sData, (float)(this._nLabelWidth - 2), f, ref fStringWidth, e.Graphics);
				e.Graphics.DrawString(sData, f, Brushes.Black, 0f + (float)((this._nLabelWidth + this._nHorizontalGap) * (nColumnIndex - 1)) + this._nOffsetX, 11f + this._nOffsetY);
				sData = string.Concat(new string[]
				{
					this._labels[this.i].Width.ToString(),
					(this._labels[this.i].SubtenseOfWidth == "") ? "" : ("/" + this._labels[this.i].SubtenseOfWidth),
					"×",
					this._labels[this.i].Hight.ToString(),
					(this._labels[this.i].SubtenseOfHight == "") ? "" : ("/" + this._labels[this.i].SubtenseOfHight)
				});
				if (this._labels[this.i].SpecialShapedName.Length > 0)
				{
					sData = this._labels[this.i].SpecialShapedName;
				}
				f.Dispose();
				f = new Font("Times New Roman", 6.5f + this._nFontSize, FontStyle.Bold, GraphicsUnit.Millimeter);
				fStringWidth = 0f;
				f = this.CalculateFitFont(sData, (float)(this._nLabelWidth - 2), f, ref fStringWidth, e.Graphics);
				e.Graphics.DrawString(sData, f, Brushes.Black, 0f + (float)((this._nLabelWidth + this._nHorizontalGap) * (nColumnIndex - 1)) + this._nOffsetX, 15f + this._nOffsetY);
				sData = this._labels[this.i].Remark + ((this._labels[this.i].Technics != "") ? ("/" + this._labels[this.i].Technics) : "");
				f.Dispose();
				f = new Font("黑体", 4f + this._nFontSize, FontStyle.Regular, GraphicsUnit.Millimeter);
				fStringWidth = 0f;
				f = this.CalculateFitFont(sData, (float)(this._nLabelWidth - 2), f, ref fStringWidth, e.Graphics);
				e.Graphics.DrawString(sData, f, Brushes.Black, 0f + (float)((this._nLabelWidth + this._nHorizontalGap) * (nColumnIndex - 1)) + this._nOffsetX, 22.5f + this._nOffsetY);
				f.Dispose();
				if (this.j < nLabelsOfItem - 1)
				{
					this.j++;
				}
				else if (this.i < this._labels.Length - 1)
				{
					this.i++;
					this.j = 0;
					nLabelsOfItem = this._labels[this.i].PrintQuantity * this._labels[this.i].PrintMultiple;
					is换行 = (sProductTypeData != this._labels[this.i].ProductType);
				}
				else
				{
					this.i++;
					this.j++;
				}
				if (nColumnIndex >= this._nColumnCountOfPage || (this._不同类型是否换行打印 && is换行))
				{
					break;
				}
			}
			if (this.i < this._labels.Length)
			{
				if (this.j < this._labels[this.i].PrintQuantity * this._labels[this.i].PrintMultiple)
				{
					e.HasMorePages = true;
				}
				else if (this.i < this._labels.Length)
				{
					e.HasMorePages = true;
				}
				else
				{
					e.HasMorePages = false;
				}
			}
		}

		private Font CalculateFitFont(string s, float fPrintAreaWidth, Font f, ref float fStringWidth, Graphics grfx)
		{
			Font resultFont = null;
			for (float i = f.Size; i > 0f; i -= 0.2f)
			{
				resultFont = new Font(f.Name, i, f.Style, f.Unit);
				SizeF sizef = grfx.MeasureString(s, resultFont);
				if (sizef.Width <= fPrintAreaWidth)
				{
					fStringWidth = sizef.Width;
					f.Dispose();
					break;
				}
				resultFont.Dispose();
			}
			return resultFont;
		}

		public override void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(this);
			base.Dispose();
		}

		protected virtual void Dispose(bool disposing)
		{
			if (!this._isDisposed)
			{
				if (disposing)
				{
					if (this._printDocument != null)
					{
						this._printDocument.Dispose();
						this._printDocument = null;
					}
				}
				this._isDisposed = true;
			}
		}

		~Printer_General_1()
		{
			this.Dispose(false);
		}
	}
}
